import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { fileURLToPath, URL } from "node:url";
import { analyzer } from "vite-bundle-analyzer";
import vitePluginImp from "vite-plugin-imp";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    vitePluginImp({
      libList: [
        {
          libName: "antd",
          style: (name) => `antd/es/${name}/style`, // 按需引入样式文件
        },
      ],
    }),
    analyzer(),
  ],
  build: {
    sourcemap: true,
    rollupOptions: {
      output: {
        // manualChunks: {
        //   edit: ["./src/pages/question/Edit/index.tsx"],
        //   stat: ["./src/pages/question/Stat/index.tsx"],
        //   antd: ["antd"],
        //   reactDom: ["react-dom"],
        // },
        manualChunks(id) {
          if (id.includes("question/Edit")) {
            return "edit";
          }
          if (id.includes("question/Stat")) {
            return "stat";
          }
          if (id.includes("/antd/")) {
            return "antd";
          }
          if (id.includes("/recharts/")) {
            return "recharts";
          }
          if (id.includes("/react-dom/")) {
            return "react-dom";
          }
          if (id.includes("/node_modules/")) {
            return "vendor";
          }
        },
      },
    },
  },
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    port: 8000,
    proxy: {
      "/api/": {
        target: "http://localhost:3005",
        changeOrigin: true,
      },
    },
  },
});
